package richbar.com.richbar.help;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.common.util.IntentUtil;
import com.common.util.LogUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import richbar.com.richbar.bean.MerchantEntryResult;
import richbar.com.richbar.bean.VersionResult;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.VersionDialog;

/**
 * Created by QiYang on 2018/12/5.
 */

public class VersionHelp {
    private Activity context;

    public VersionHelp(Activity context) {
        this.context = context;
    }

    public void update(final boolean isTipIsLatestVersion){
        NetWorkUtil.getVersion("1", Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final VersionResult result = MyGson.getInstance().fromJson(data, VersionResult.class);
                if (result.getCode().equals("200") &&
                        null != result.getData() &&
                        !result.getData().getInfo().getVersion().equals(Constants.VERSION)){
                    showDialog(result);
                }else{
                    if (isTipIsLatestVersion){
                        ToastUtil.showToast(context,"当前已是最新版本!");
                    }
                }
            }
        });
    }

    private void showDialog(VersionResult result) {
        VersionDialog.show(context, result,new VersionDialog.OnRightListener() {
            @Override
            public void onBesure() {
                IntentUtil.toWeb(context,NetWorkUtil.URL);
            }
        });
    }
}
